package com.daocao.yfb.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daocao.yfb.R;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.OrderDetailBean;
import com.daocao.yfb.api.model.OrderDetailModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.util.DateTime;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.view.V19FrameLayout;

import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class OrderDetailActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.view_line_left)
    View viewLineLeft;
    @BindView(R.id.view_line_center)
    View viewLineCenter;
    @BindView(R.id.view_line_right)
    View viewLineRight;
    @BindView(R.id.txt_num_one)
    TextView txtNumOne;
    @BindView(R.id.txt_num_two)
    TextView txtNumTwo;
    @BindView(R.id.txt_num_three)
    TextView txtNumThree;
    @BindView(R.id.txt_num_four)
    TextView txtNumFour;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.txt_id)
    TextView txtId;
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_num)
    TextView txtNum;
    @BindDrawable(R.drawable.circle_blue)
    Drawable circleBlue;
    @BindDrawable(R.drawable.circle_grey)
    Drawable circleGrey;
    @BindColor(R.color.main_blue)
    int colorBlue;
    @BindColor(R.color.grey)
    int colorGrey;
    @BindColor(R.color.sec_black)
    int colorBlack;
    @BindColor(R.color.white)
    int colorWhite;
    @BindView(R.id.txt_total_order)
    TextView txtTotalOrder;
    @BindView(R.id.txt_now_pay)
    TextView txtNowPay;
    @BindView(R.id.txt_order_num)
    TextView txtOrderNum;
    @BindView(R.id.txt_create_time)
    TextView txtCreateTime;
    @BindView(R.id.txt_success_time)
    TextView txtSuccessTime;
    @BindView(R.id.txt_title_order)
    TextView txtTitleOrder;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("订单详情");
        String relateId = getIntent().getStringExtra("relate_id");
        String order_number = getIntent().getStringExtra("order_number");
        getOrderDetail(order_number, relateId);
    }

    public void getOrderDetail(String orderNum, String id) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        OrderDetailBean bean = new OrderDetailBean();
        bean.order_number = orderNum;
        bean.relate_id = id;
        Api.api_service.getOrderDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });

    }

    private void setData(OrderDetailModel.DatasBean item) {
        getStatus(item.status);

        Glide.with(this).load(item.icon).into(imgIcon);
        txtMoney.setText("￥" + item.money);
        txtTime.setText(DateTime.toDateTime(item.created_at*1000));
        txtTitleOrder.setText(item.name);
        txtName.setText(item.merchant_name);
        if (item.relate_id.equals("0")) {
            txtId.setText("未绑定云联惠");
        } else {
            txtId.setText("云联惠ID：" + item.relate_account);
        }
        txtNum.setText("x" + item.num);
        txtTotalOrder.setText("￥" + item.money_sum);
        txtNowPay.setText("￥" + item.money_sum);
        txtOrderNum.setText("订单编号:" + item.order_number);
        txtCreateTime.setText("创建时间:" + DateTime.toDateTime(item.created_at*1000));
        txtSuccessTime.setText("成交时间:" + DateTime.toDateTime(item.pay_date*1000));

    }


    private void getStatus(String status) {
        switch (status) {
            case "0":
                txtStatus.setText("待支付");
                break;
            case "1":
                txtNumOne.setBackgroundDrawable(circleBlue);
                txtNumOne.setTextColor(colorWhite);
                viewLineLeft.setBackgroundColor(colorBlue);
                txtNumTwo.setBackgroundDrawable(circleBlue);
                txtNumTwo.setTextColor(colorWhite);
                viewLineCenter.setBackgroundColor(colorBlue);
                txtNumThree.setBackgroundDrawable(circleBlue);
                txtNumThree.setTextColor(colorWhite);
                viewLineRight.setBackgroundColor(colorBlue);
                txtNumFour.setBackgroundDrawable(circleBlue);
                txtNumFour.setTextColor(colorWhite);
                txtStatus.setText("已到账");
                break;
            case "-1":
                txtStatus.setText("交易关闭");
                break;
            case "2":
                txtNumOne.setBackgroundDrawable(circleBlue);
                txtNumOne.setTextColor(colorWhite);
                txtStatus.setText("已付款");
                break;
            case "3":
                txtNumOne.setBackgroundDrawable(circleBlue);
                txtNumOne.setTextColor(colorWhite);
                viewLineLeft.setBackgroundColor(colorBlue);
                txtNumTwo.setBackgroundDrawable(circleBlue);
                txtNumTwo.setTextColor(colorWhite);
                txtStatus.setText("发货中");
                break;
            case "4":
                txtNumOne.setBackgroundDrawable(circleBlue);
                txtNumOne.setTextColor(colorWhite);
                viewLineLeft.setBackgroundColor(colorBlue);
                txtNumTwo.setBackgroundDrawable(circleBlue);
                txtNumTwo.setTextColor(colorWhite);
                viewLineCenter.setBackgroundColor(colorBlue);
                txtNumThree.setBackgroundDrawable(circleBlue);
                txtNumThree.setTextColor(colorWhite);
                txtStatus.setText("已发货");
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
